package br.com.ordersys.framework.utils;

public class StringUtils {
	final protected static char[] hexArray = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'e' };

	public static String emptyAsNull(String s) {
		if (s == null) {
			return null;
		}
		s = s.trim();

		return s.length() == 0 ? null : s;
	}

	public static void replace(String toFind, String replaceWith, StringBuilder onBuf) {
		int startIndex = 0;
		int foundIndex = 0;

		if (replaceWith == null) {
			replaceWith = "";
		}

		int sizeOfReplace = replaceWith.length();
		int sizeOfFind = toFind.length();

		while ((foundIndex = onBuf.indexOf(toFind, startIndex)) > -1) {
			onBuf.replace(foundIndex, foundIndex + sizeOfFind, replaceWith);
			startIndex = foundIndex + sizeOfReplace;
		}
	}
	
	public static String bytesToHex(byte[] bytes) {
		char[] hexChars = new char[bytes.length * 2];
		int v;
		for (int j = 0; j < bytes.length; j++) {
			v = bytes[j] & 0xFF;
			hexChars[j * 2] = hexArray[v >>> 4];
			hexChars[j * 2 + 1] = hexArray[v & 0x0F];
		}
		return new String(hexChars);
	}

	public static void main(String[] args) {
		StringBuilder b = new StringBuilder();
		b.append("select {teste} from where qualquer coisa {teste} e mais");

		replace("{teste}", "", b);

		System.out.println(b);
	}
}
